package com.yeyks.app.controller.base.consumer;

import cn.hutool.extra.servlet.ServletUtil;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.consumer.UserIdParam;
import com.yeyks.commonReference.service.consumer.CashOutOrderService;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.param.CashOutOrder.CashOutOrderBindWxParam;
import com.yeyks.consumer.param.CashOutOrderExtractParam;
import com.yeyks.consumer.service.UserInfoService;
import com.yeyks.consumer.service.UserInviteWhiteListService;
import com.yeyks.consumer.service.UserWxidRelationService;
import com.yeyks.consumer.vo.CashOutOrder.CashOutOrderIsBindWxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/26 0026
 */
@Api(tags = "提现接口", description = "提现")
@Slf4j
@RestController
@RequestMapping(value = "/app/cash")
public class CashOutOrderController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CashOutOrderService cashOutOrderService;
    @Autowired
    private UserWxidRelationService userWxidRelationService;
    @Autowired
    private UserInviteWhiteListService userInviteWhiteListService;

    /**
     * 判断用户是否需要绑卡
     *
     * @param token
     * @return
     */
    @ApiOperation(value = "判断用户是否需要绑卡", notes = "true需要绑卡，false不需要绑卡")
    @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String")
    @PostMapping(value = "/isBind")
    public AjaxResponseBody<Boolean> isBind(@RequestParam(name = "token") String token) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        Boolean flag = userInfoService.isBind(userInfo);
        return AjaxResponseBody.success(flag);
    }

    @ApiOperation(value = "是否已绑定微信:v3.7.3", notes = "用户提现")
    @PostMapping(value = "/is/bind/wx")
    public AjaxResponseBody<CashOutOrderIsBindWxVo> isBindWx(@LoadUserId UserIdParam param) {
        return AjaxResponseBody.success(userWxidRelationService.isBindWx(param.getUserId()));
    }

    @ApiOperation(value = "绑定微信:v3.7.3", notes = "用户提现")
    @PostMapping(value = "/bind/wx")
    public AjaxResponseBody<Boolean> bindWx(@Validated @LoadUserId CashOutOrderBindWxParam param) {
        Boolean b = userWxidRelationService.bindWx(param);
        if (!b) {
            throw new ServiceException("一个微信不支持绑定多个账户");
        }
        return AjaxResponseBody.success(b);
    }

    @ApiOperation(value = "用户提现:v3.7.3", notes = "用户提现")
    @PostMapping(value = "/extract")
    public AjaxResponseBody<Boolean> extract(@Validated @LoadUserId CashOutOrderExtractParam param, HttpServletRequest request) {
        param.setIp(ServletUtil.getClientIP(request));
        return AjaxResponseBody.success(cashOutOrderService.extract(param));
    }

    @ApiOperation(value = "0元白名单:v3.8", notes = "0元白名单")
    @PostMapping(value = "/is/white")
    public AjaxResponseBody<Map<String, Object>> isWhite(@LoadUserId UserIdParam param) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = userInviteWhiteListService.isWhite(param.getUserId());
        map.put("flag", b);
        map.put("url", "https://kuaisu.chengguokj.com/other/pushHand");
        return AjaxResponseBody.success(map);
    }

    /**
     * 提现常见问题
     *
     * @return
     */
    @ApiOperation(value = "提现常见问题", notes = "提现常见问题")
    @PostMapping(value = "/problem")
    public AjaxResponseBody<?> problem() {
        return AjaxResponseBody.success(cashOutOrderService.problem());
    }
}
